package cn.yunhe.service.impl;

import java.util.List;

import cn.yunhe.Utils.PageBean;
import cn.yunhe.Utils.UUIDUtiils;
import cn.yunhe.bean.Orderitem;
import cn.yunhe.bean.Orders;
import cn.yunhe.bean.OrdersExample;
import cn.yunhe.mapper.OrderitemMapper;
import cn.yunhe.mapper.OrdersMapper;
import cn.yunhe.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Service
public class OrdersServiceimpl implements OrdersService {

	@Resource
	private OrdersMapper ordersMapper;
	@Resource
	private OrderitemMapper orderitemMapper;

	@Override
	public void toOrder(Orders orders) {
		// TODO Auto-generated method stub
		ordersMapper.insert(orders);

	}

	@Override
	public void toOrderItem(Orderitem orderitem) {
		// TODO Auto-generated method stub
		orderitemMapper.insert(orderitem);

	}
	@Override
	public Orders findOrderByOid(int oid) {
		return ordersMapper.selectByPrimaryKey(oid);

	}

	@Override
	public PageBean<Orders> findOrderByUidAndPage(int page, Integer uid){
		PageBean<Orders> pageBean = new PageBean<>();
//		设置这是第几页
		pageBean.setPage(page);
//		设置10个
		int limitPage =4;
		pageBean.setLimitPage(limitPage);
//		设置一共多少页
		int totlePage = 0;
//		查询一共有多少页
		totlePage = ordersMapper.countOrdersByUid(uid);
		if(Math.ceil(totlePage % limitPage)==0){
			totlePage=totlePage / limitPage;
		}else{
			totlePage=totlePage / limitPage+1;
		}
		pageBean.setTotlePage(totlePage);
		int beginPage= (page-1)*limitPage;
//		商品集合
		List<Orders> list = ordersMapper.findOrderByUidAndPage(uid,beginPage,limitPage);
		pageBean.setList(list);
		return pageBean;
	}

	//提交订单
	@Override
	public void payOrder(Orders orders, HttpSession session){
//		Orders payOrder = ordersMapper.selectByPrimaryKey(orders.getOid());
//		if(orders.getReceiveinfo()!="" && orders.getPhonum()!=""){
//			System.out.println("id得值"+ordersMapper.selectByPrimaryKey(orders.getOid()));
//			payOrder.setReceiveinfo(orders.getReceiveinfo());
//			payOrder.setPhonum(orders.getPhonum());
//			payOrder.setAccepter(orders.getAccepter());
//			payOrder.setState(1);
//
//		}
//		ordersMapper.updateByPrimaryKeySelective(payOrder);

		ordersMapper.insert(orders);
		List pid = (List) session.getAttribute("pid");
		List<Orders> findnews = ordersMapper.findnews(pid.size());
		for (int i = 0 ;i<pid.size();i++){
			Orderitem orderitem = new Orderitem();
			orderitem.setOid(findnews.get(i).getOid());
			orderitem.setPid((Integer) pid.get(i));
			orderitem.setCount(1);
			orderitem.setSubtotal(orders.getMoney());
			orderitemMapper.insertSelective(orderitem);
		}


//		pid.forEach((id)->{
//
//			Orderitem orderitem = new Orderitem();
//			orderitem.setOid(orders.getOid());
//			orderitem.setPid((Integer) id);
//			orderitem.setCount(1);
//			orderitem.setSubtotal(orders.getMoney());
//					orderitemMapper.insertSelective(orderitem);
//		}
//		);


	}

	@Override
	public PageBean<Orders> findAllOrderByStateAndPage(int page){
		PageBean<Orders> pageBean = new PageBean<>();
//		设置这是第几页
		pageBean.setPage(page);
//		设置10个
		int limitPage = 4;
		pageBean.setLimitPage(limitPage);
//		设置一共多少页
		int totlePage = 0;
//		查询一共有多少页
		totlePage = ordersMapper.countAllOrders();
		if(Math.ceil(totlePage % limitPage)==0){
			totlePage=totlePage / limitPage;
		}else{
			totlePage=totlePage / limitPage+1;
		}
		pageBean.setTotlePage(totlePage);
		int beginPage= (page-1)*limitPage;
		//商品集合
		List<Orders> list = ordersMapper.findAllOrderByPage(beginPage,limitPage);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public void updateOrderStatus(int oid, int status) {
		Orders payOrder = ordersMapper.selectByPrimaryKey(oid);
		payOrder.setState(status);
		ordersMapper.updateByPrimaryKeySelective(payOrder);
	}

	@Override
	public PageBean<Orders> findAllOrderByStateAndPage(int state,int page){
		PageBean<Orders> pageBean = new PageBean<>();
//		设置这是第几页
		pageBean.setPage(page);
//		设置10个
		int limitPage =4;
		pageBean.setLimitPage(limitPage);
//		设置一共多少页
		int totlePage = 0;
//		查询一共有多少页
		totlePage = ordersMapper.countOrdersByState(state);

		if(Math.ceil(totlePage % limitPage)==0){
			totlePage=totlePage / limitPage;
		}else{
			totlePage=totlePage / limitPage+1;
		}
		pageBean.setTotlePage(totlePage);
		int beginPage= (page-1)*limitPage;
		//商品集合
		List<Orders> list = ordersMapper.findAllOrderByStateAndPage(state, beginPage, limitPage) ;
		pageBean.setList(list);
		return pageBean;
	}




	public PageBean<Orders> findall(int page){
		OrdersExample ordersExample = new OrdersExample();
		List<Orders> orders = ordersMapper.selectByExample(ordersExample);

		PageBean<Orders> pageBean = new PageBean<>();
//		设置这是第几页
		pageBean.setPage(page);
//		设置10个
		int limitPage =4;
		pageBean.setLimitPage(limitPage);
//		设置一共多少页
		int totlePage = 0;
//		查询一共有多少页
		totlePage = ordersMapper.countAllOrders();

		if(Math.ceil(totlePage % limitPage)==0){
			totlePage=totlePage / limitPage;
		}else{
			totlePage=totlePage / limitPage+1;
		}
		pageBean.setTotlePage(totlePage);
		int beginPage= (page-1)*limitPage;
		//商品集合
		List<Orders> list = ordersMapper.findmyord(beginPage, limitPage) ;
		pageBean.setList(list);

		return pageBean;
	}




}
